package com.region.loadbalancer.policy;

import com.region.loadbalancer.group.Server;

import java.util.List;

/**
 * Custom Weighting Strategy
 *
 * @author liujieyu
 * @date 2023/5/27 15:07
 * @desciption
 */
public class CustomWeightPolicy extends WeightPolicy {

    protected int[] weight;

    public CustomWeightPolicy() {
        this(1);
    }

    public CustomWeightPolicy(int... value) {
        this.weight = value;
    }

    @Override
    protected void setAllWeight(List<Server> servers) throws UnsupportedOperationException {
        // do nothing
    }

    @Override
    protected int getWeightByIndex(int index) throws UnsupportedOperationException {
        // Load Really Weight
        return weight[index % weight.length];
    }

    @Override
    public BalancerPolicy deepClone() {
        return new CustomWeightPolicy(this.weight);
    }
}
